<template>
    <el-form :model="form" ref="formRef" label-width="80px" v-loading="formLoading">
        <el-form-item label="上级分类" prop="parent">
            <el-input v-model="form.parentName" disabled />
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
            <el-input v-model="form.name" clearable />
        </el-form-item>
        <el-form-item label="层级" prop="name">
            <el-input-number v-if="isEdit" v-model="form.catLevel" :min="1" :max="3" clearable />
            <el-input-number v-else v-model="form.catLevel" disabled />
        </el-form-item>
        <el-form-item label="是否展示" prop="name">
            <el-switch v-model="form.showStatus" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="排序" prop="name">
            <el-input-number v-model="form.sort" />
        </el-form-item>
        <el-form-item label="图标地址" prop="name">
            <el-input v-model="form.icon" clearable />
        </el-form-item>
        <el-form-item label="计量单位" prop="name">
            <el-input v-model="form.productUnit" clearable />
        </el-form-item>
        <el-form-item label="商品数量" prop="name">
            <el-input-number v-model="form.productCount" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submit" :loading="loading">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts" name="AddOrEditPmsCategory">
import { ref, onMounted, toRefs } from 'vue';
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus';
import { pmsCategoryGetById, pmsCategoryCreateOrEdit } from '@/api/mall/pmsCategory';
import type { pmsCategoryType } from '@/api/mall/pmsCategory/types';
import { reactive } from 'vue';

const props = defineProps(['categoryFormData'])
const emit = defineEmits(['success'])

const loading = ref(false)
const formLoading = ref(false)
let form = reactive<pmsCategoryType>({ showStatus: 0, sort: 1, productCount: 1 })
const formRef = ref<FormInstance>()
const { isEdit, catId, parentCid, catLevel } = toRefs(props.categoryFormData)

onMounted(() => {
    // 加载的时候 如果编辑
    if (isEdit.value) {
        loadCategory()
    } else {
        // 如果是添加
        form.parentCid = parentCid.value
        form.catLevel = catLevel.value
        if (form.parentCid === 0) {
            form.parentName = '无'
        } else {
            form.parentName = props.categoryFormData.name
        }
    }
})

const loadCategory = async () => {
    try {
        formLoading.value = true
        const data = await pmsCategoryGetById(catId.value)
        Object.assign(form, data.data)
    } finally {
        formLoading.value = false
    }
}

const submit = async () => {
    await formRef.value?.validate()
    try {
        loading.value = true
        const data = await pmsCategoryCreateOrEdit(form)
        if (data.code === 200) {
            ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
            emit('success')
        }
    } finally {
        loading.value = false
    }
}
</script>

<style scoped lang="scss"></style>